﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ServerLibrary.Abstract;

namespace ServerLibrary.Concrete
{
    /// <summary>
    /// Implements file handler.
    /// </summary>
    public class FileHandler : IHandler
    {

        public FileHandler(List<string> fileTypes, string serverRoot)
        {
            this.FileTypes = fileTypes;
            this.ServerRoot = serverRoot;
        }

        /// <summary>
        /// Gets or sets permissible fyle types.
        /// </summary>
        public List<string> FileTypes { get { return fileTypes; }  set{ fileTypes = value; } }

        /// <summary>
        /// Get or sets path to documents root folder.
        /// </summary>
        public string ServerRoot { get { return serverRoot; } set { serverRoot = value; } }

        #region IHandler implements

        public IHandleResult Handle(string fileName)
        {
            int httpStatus = 200;
            string contentType = "";
            byte[] file = null;
            string fileExtension = fileName.Substring(fileName.LastIndexOf('.'));
            contentType = GetContentType(fileExtension);
            if (contentType == "")
                httpStatus = 404;
            else
            {
                foreach (string fileType in fileTypes)
                {
                    if (fileType == fileExtension)
                    {
                        try
                        {
                            using (FileStream requestedFile = new FileStream(ServerRoot + fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                file = new byte[requestedFile.Length];
                                requestedFile.Read(file, 0, file.Length);
                                requestedFile.Close();
                                break;
                            }
                        }
                        catch (IOException)
                        {
                            httpStatus = 404;
                            break;
                        }
                    }
                }
            }
            return new HandleResult() {File = file, ContentType = contentType, HttpStatusCode = httpStatus};
        }

        #endregion

        /// <summary>
        /// Gets content type.
        /// </summary>
        /// <param name="fileExtension">File extension.</param>
        /// <returns>Content type or "".</returns>
        public string GetContentType(string fileExtension)
        {
            string contentType;
            switch (fileExtension)
            {
                case ".htm":
                case ".html":
                    contentType = "text/html";
                    break;
                case ".jpg":
                    contentType = "image/jpeg";
                    break;
                case ".jpeg":
                case ".png":
                case ".gif":
                    contentType = "image/" + fileExtension.Substring(1);
                    break;
                case ".css":
                    contentType = "text/stylesheet";
                    break;
                case ".js":
                    contentType = "text/javascript";
                    break;
                default:
                    contentType = "";
                    break;
            }
            return contentType;
        }

        /// <summary>
        /// Contains permissible fyle types.
        /// </summary>
        private List<string> fileTypes;

        /// <summary>
        /// Contains path to documents root folder.
        /// </summary>
        private string serverRoot;
    }
}
